<template>
    <div id="OFD"></div>
</template>
<script>
import { suwell } from '@/utils/shukeOfd/suwell_ofdreader'
import { http } from "@/utils/request";
import $ from 'jquery';
import axios from "axios";
import { getToken } from "@/utils/auth";
import { sydm, getKeyWord } from "@/api/archive/fileCollection/offlinReception";
import { minusOne, minusOnePrint, selectPrint } from "@/api/archive/archiveBorrow/archiveBorrow";
export default {
    props: ["ofdUrl", "item",'borrowItemId'],
    data() {
        return {
            data: "",
            synr: "",
            ocx: undefined
        }
    },
    watch: {
        item: {
            handler: function (newVal) {
                // console.log(newVal, "liuhairong---------------------")
                
            },
            immediate: true,
        }
    },
    created(){
     
    },
    mounted() {
        var that = this;
        that.ocx = suwell.ofdReaderInit('OFD', "100%", (window.screen.availHeight - 120));
        if (that.ocx) {
            var time = setInterval(function () {
                if (that.ocx.version()) {
                    clearInterval(time);
                    var element = document.getElementById("OFD");
                    var divs = element.getElementsByTagName("embed");
                    for (var i = 0; i < divs.length; i++) {
                        divs[i].style.height = '100%'; // 设置高度为200像素
                    }
                    that.ocx.setCompsiteVisible("open", false);//隐藏打开
                    that.ocx.setCompsiteVisible("save", false);//隐藏保存
              
                    // var url = "http://192.168.2.24:8088/api/JspAction?method=FileToBase" + "&CMDLine=" + that.data
                    var url = ""
                    if (that.item.borrowIdofd != undefined) {  //借阅详情进入预览ofd
                        selectPrint(that.borrowItemId).then(res=>{   //打印次数    借阅目录功能
                        if(res.data>0){
                            that.ocx.setCompsiteVisible("f_print",true);//显示打印
                        }else{
                            that.ocx.setCompsiteVisible("f_print",false); //隐藏打印
                        }
                        })
                        url = window.location.protocol + '//' + window.location.host + process.env.VUE_APP_BASE_API + `/archive/archiveBorrow/viewOfd?token=` + that.ofdUrl
                    } else {
                        url = window.location.protocol + '//' + window.location.host + process.env.VUE_APP_BASE_API + `/archive/detail/viewOfd?token=` + that.ofdUrl
                    }
                    // console.log(url, that.item.borrowIdofd, "00000000000000000000000000")
                    that.ocx.registListener('f_open', 'oepnListener', true) //监听打开事后事件, true=事件后，falses=事件前
                    that.ocx.registListener("f_print", "printFile", true) //打印回调
                    that.ocx.openFile(url, false)
                }
            }, 100);
        }

        window['oepnListener'] = function () {
          
            console.log(that.borrowItemId,"打开")
           
           
            sydm({ //水印接口
                sydm: "yulan"
            }).then(res => {
                console.log(res.data, "水印")
                that.synr = res.data.synr;
                var data = '<?xml version="1.0" encoding="UTF-8"?>'
                    + '<setinfo type="barinfo">'
                    + '<parameter name="pages" value="1-2000"/>'
                    + '<parameter name="rotate" value="0"/>'
                    + '<parameter name="visible" value="true"/>'
                    + '<parameter name="printable" value="true"/>'
                    + '<parameter name="xpostype" value="absolute"/>'
                    + '<parameter name="ypostype" value="absolute"/>'
                    + '<parameter name="x" value="10"/>'
                    + '<parameter name="y" value="10"/>'
                    + '<parameter name="w" value="100"/>'
                    + '<parameter name="h" value="20"/>'
                    + '<parameter name="strcont" font="仿宋" size="48" opaque="true" color="DCDFE6" >' + res.data.synr + '</parameter>'
                    + '</setinfo>'

                console.log("that.item.borrowId", that.item.borrowId)
                if (that.item.borrowIdofd != undefined) {
                    getKeyWord({ //遮盖接口
                        docId: that.item.DOC_ID,
                        borrowId: that.item.borrowId
                    }).then(res => {
                        console.log(res)
                        if (res.code == "200") {
                            var keywordinfos = [];
                            for (var i in res.data) {
                                keywordinfos.push({
                                    "keyword": res.data[i],
                                    "permissions": "archive"
                                })
                            }

                            var dataJson = {
                                "type": "keyword",
                                "keywordinfos": keywordinfos
                            }
                            console.log(dataJson, "===========================keywordinfos")
                            var color = '{"color":"#000","transparency":"90"}';
                            that.ocx.setMarkContentAppearance(color);//遮盖颜色
                            that.ocx.batchCovering(JSON.stringify(dataJson)); //关键字遮盖
                        }

                    })
                }
                // {"type":"keyword","keywordinfos":[{"keyword":"All","permissions":"archive"},{"keyword":"B","permissions":"archive"}]}
                // that.ocx.batchCovering(JSON.stringify(dataJson)); //关键字遮盖
                that.ocx.addTrackInfo(data);
            })


        }

        window['printFile'] = function () {
           
            minusOnePrint(that.borrowItemId).then(data=>{
                console.log("----------------------------------------------------")
                selectPrint(that.borrowItemId).then(res=>{   //打印次数
                        if(res.data>0){
                            that.ocx.setCompsiteVisible("f_print",true);//显示打印
                        }else{
                            that.ocx.setCompsiteVisible("f_print",false); //隐藏打印
                        }
                        })
            })
            
            // setCompsiteEnable(String cmpName,boolean isEnable);//设置界面元素可用性
        }




    },
    created() {
    },

    methods: {
        //打印次数
        printNum(borrowItemId) {
            selectPrint(borrowItemId).then(res=>{  
            console.log(this.item,"----------------------------")
            })
        }
    },
    beforeDestroy() {
        if (this.stopListening) {
            // 清理代码，例如移除事件监听器
            clearInterval(time);
        }
    }

}
</script>
<style lang="less" scoped></style>
